home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / IE3 / CLASSES.ZIP / sun / NET / WWW / PROTOCOL / HTTP / HttpURLConnection.class (.txt) < prev   
Encoding:
Java Class File  |  1997-03-06  |  4.7 KB  |  192 lines

  1. package sun.net.www.protocol.http;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintStream;
  7. import java.net.URL;
  8. import sun.net.ProgressData;
  9. import sun.net.www.MessageHeader;
  10. import sun.net.www.MeteredStream;
  11. import sun.net.www.URLConnection;
  12. import sun.net.www.http.AuthenticationInfo;
  13. import sun.net.www.http.HttpClient;
  14.  
  15. public class HttpURLConnection extends URLConnection {
  16.    static final String EOL = "\r\n";
  17.    static final String version = System.getProperty("java.version");
  18.    public static final String userAgent;
  19.    static final String acceptString = "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
  20.    static final int maxRedirections = 5;
  21.    HttpClient http;
  22.    Handler handler;
  23.    InputStream inputStream;
  24.    boolean outputStreamOpen = false;
  25.  
  26.    HttpURLConnection(URL var1, Handler var2) {
  27.       super(var1);
  28.       this.handler = var2;
  29.    }
  30.  
  31.    public HttpURLConnection(URL var1, String var2, int var3) {
  32.       super(var1);
  33.       this.handler = new Handler(var2, var3);
  34.    }
  35.  
  36.    public void connect() throws IOException {
  37.       if (!super.connected) {
  38.          super.connected = true;
  39.          ProgressData.pdata.register(super.url);
  40.          this.http = null;
  41.  
  42.          try {
  43.             if (super.url.getProtocol().equals("https")) {
  44.                throw new IOException("https not supported for this version.");
  45.             }
  46.  
  47.             this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
  48.          } catch (Throwable var2) {
  49.             if (this.http != null) {
  50.                this.http.closeServer();
  51.             }
  52.  
  53.             ProgressData.pdata.unregister(super.url);
  54.             if (var2 instanceof SecurityException) {
  55.                throw (SecurityException)var2;
  56.             }
  57.  
  58.             throw var2 instanceof IOException ? (IOException)var2 : new IOException(var2.toString());
  59.          }
  60.  
  61.          if (this.http == null) {
  62.             throw new IOException("Couldn't connect to " + super.url.toExternalForm());
  63.          }
  64.       }
  65.    }
  66.  
  67.    private static AuthenticationInfo getAuthentication(URL var0, String var1, String var2) {
  68.       return null;
  69.    }
  70.  
  71.    public OutputStream getOutputStream() throws IOException {
  72.       String var1 = ((java.net.URLConnection)this).getRequestProperty("content-type");
  73.       this.connect();
  74.       if (this.inputStream != null) {
  75.          throw new IOException("Cannot write output after reading input.");
  76.       } else if (this.outputStreamOpen) {
  77.          throw new IOException("Cannot open output twice.");
  78.       } else {
  79.          HttpClient var2 = this.http;
  80.          PrintStream var5 = var2.serverOutput;
  81.          this.outputStreamOpen = true;
  82.          PrintStream var3 = new PrintStream(var5);
  83.          String var4 = "POST " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + "Referer: " + super.url + "\r\n" + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
  84.          if (var1 == null) {
  85.             var1 = "application/x-www-form-urlencoded";
  86.          }
  87.  
  88.          var4 = var4 + "Content-type: " + var1 + "\r\n";
  89.          var3.print(var4);
  90.          return new HttpPostBufferStream(var5);
  91.       }
  92.    }
  93.  
  94.    public InputStream getInputStream() throws IOException {
  95.       if (this.inputStream != null) {
  96.          return this.inputStream;
  97.       } else {
  98.          int var2 = 0;
  99.  
  100.          while(true) {
  101.             this.connect();
  102.             if (!this.outputStreamOpen) {
  103.                String var3 = "GET " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + "" + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n" + "\r\n";
  104.                HttpClient var4 = this.http;
  105.                PrintStream var10 = var4.serverOutput;
  106.                var10.print(var3);
  107.                var10.flush();
  108.             }
  109.  
  110.             try {
  111.                this.http.processRequest(super.url.getFile());
  112.                HttpClient var7 = this.http;
  113.                Object var1 = var7.serverInput;
  114.                var7 = this.http;
  115.                String var9 = var7.mimeHeader != null ? var7.mimeHeader.findValue("location") : null;
  116.                if (var9 != null) {
  117.                   if (this.outputStreamOpen) {
  118.                      this.http.closeServer();
  119.                      super.connected = false;
  120.                      this.http = null;
  121.                      throw new IOException("Cannot redirect with POST: " + super.url);
  122.                   }
  123.  
  124.                   ++var2;
  125.                   if (var2 > 5) {
  126.                      throw new IOException("Too many redirections (" + var2 + "): " + super.url);
  127.                   }
  128.  
  129.                   ProgressData.pdata.unregister(super.url);
  130.                   URL var11 = new URL(super.url, var9);
  131.                   super.url = var11;
  132.                   if (!"http".equals(super.url.getProtocol())) {
  133.                      this.http.closeServer();
  134.                      super.connected = false;
  135.                      this.http = null;
  136.                      throw new IOException("Cannot redirect to other protocol: " + super.url);
  137.                   }
  138.  
  139.                   ProgressData.pdata.register(super.url);
  140.                }
  141.  
  142.                HttpClient var12 = this.http;
  143.                if (var12.status == 200 || var9 == null) {
  144.                   var12 = this.http;
  145.                   ((URLConnection)this).setProperties(var12.mimeHeader);
  146.                   if (((URLConnection)this).getProperties() == null) {
  147.                      ((URLConnection)this).setProperties(new MessageHeader());
  148.                   }
  149.  
  150.                   var12 = this.http;
  151.                   String var15 = var12.mimeHeader != null ? var12.mimeHeader.findValue("content-length") : null;
  152.                   int var5 = 0;
  153.                   if (var15 != null && (var5 = Integer.parseInt(var15)) != 0) {
  154.                      var1 = new MeteredStream((InputStream)var1, var5, super.url);
  155.                   } else {
  156.                      ProgressData.pdata.unregister(super.url);
  157.                   }
  158.  
  159.                   this.inputStream = (InputStream)var1;
  160.                   return (InputStream)var1;
  161.                }
  162.  
  163.                this.http.closeServer();
  164.                ProgressData.pdata.unregister(super.url);
  165.                this.http = null;
  166.                this.inputStream = null;
  167.                super.connected = false;
  168.             } catch (IOException var6) {
  169.                ProgressData.pdata.unregister(super.url);
  170.                throw var6;
  171.             }
  172.          }
  173.       }
  174.    }
  175.  
  176.    public String getHeaderField(String var1) {
  177.       if (this.http == null) {
  178.          try {
  179.             this.getInputStream();
  180.          } catch (Exception var3) {
  181.          }
  182.       }
  183.  
  184.       HttpClient var2 = this.http;
  185.       return var2.mimeHeader != null ? var2.mimeHeader.findValue(var1) : null;
  186.    }
  187.  
  188.    static {
  189.       userAgent = "User-Agent: " + System.getProperty("http.agent", "Java" + version) + "\r\n";
  190.    }
  191. }
  192.